//package com.learning.hadoop.sqoop.client;
//
//import org.apache.sqoop.client.SqoopClient;
//import org.apache.sqoop.model.MConnectionForms;
//import org.apache.sqoop.model.MConnector;
//import org.apache.sqoop.model.MJob;
//import org.apache.sqoop.model.MJobForms;
//import org.apache.sqoop.model.MSubmission;
//import org.apache.sqoop.submission.counter.Counter;
//import org.apache.sqoop.submission.counter.CounterGroup;
//import org.apache.sqoop.submission.counter.Counters;
//import org.apache.sqoop.validation.Status;
//
//public class Sqoop2Access {
//    public static void main(String[] args) {
//        //1、--------------Initialization（创建sqoop client）-------------------
//        String url = "http://localhost:12000/sqoop/";
//        SqoopClient client = new SqoopClient(url);
//        client.setServerUrl(url);
//
//        //2、--------------Create Connection--------------
//        //Dummy connection object
//        MConnector connector = client.getConnector(1);//1为指定的ConnectionID
//
//        //Get connection and framework forms. Set name for connection
//        MConnectionForms conForms = connector.getConnectorPart();
//        MConnectionForms frameworkForms = connector.getFrameworkPart();
//        newCon.setName("MyConnection");
//
//        //Set connection forms values
//        conForms.getStringInput("connection.connectionString").setValue("jdbc:mysql://localhost/mysqlTestDB");
//        conForms.getStringInput("connection.jdbcDriver").setValue("com.mysql.jdbc.Driver");
//        conForms.getStringInput("connection.username").setValue("root");
//        conForms.getStringInput("connection.password").setValue("root");
//
//        frameworkForms.getIntegerInput("security.maxConnections").setValue(0);
//
//
//        Status status  = client.createConnection(newCon);
//        //检测链接的相关状态
//        if(status.canProceed()) {
//         System.out.println("Created. New Connection ID : " +newCon.getPersistenceId());
//        } else {
//         System.out.println("Check for status and forms error ");
//        }
//
//        //3、--------------create job(import job)--------------
//        //Creating dummy job object(利用connectionID去创建job)
//        MJob newjob = client.newJob(newCon.getPersistenceId(), org.apache.sqoop.model.MJob.Type.IMPORT);
//        MJobForms connectorForm = newjob.getConnectorPart();
//        MJobForms frameworkForm = newjob.getFrameworkPart();
//
//        newjob.setName("ImportJob");
//        //Database configuration
//        connectorForm.getStringInput("table.schemaName").setValue("");
//        //Input either table name or sql
//        connectorForm.getStringInput("table.tableName").setValue("table");
//        //connectorForm.getStringInput("table.sql").setValue("select id,name from table where ${CONDITIONS}");
//        connectorForm.getStringInput("table.columns").setValue("id,name");
//        connectorForm.getStringInput("table.partitionColumn").setValue("id");
//        //Set boundary value only if required
//        //connectorForm.getStringInput("table.boundaryQuery").setValue("");
//
//        //Output configurations
//        frameworkForm.getEnumInput("output.storageType").setValue("HDFS");
//        frameworkForm.getEnumInput("output.outputFormat").setValue("TEXT_FILE");//Other option: SEQUENCE_FILE
//        frameworkForm.getStringInput("output.outputDirectory").setValue("/output");
//
//        //Job resources
//        frameworkForm.getIntegerInput("throttling.extractors").setValue(1);
//        frameworkForm.getIntegerInput("throttling.loaders").setValue(1);
//
//        Status statusJob = client.createJob(newjob);
//        //检测作业状态相关信息
//        if(statusJob.canProceed()) {
//         System.out.println("New Job ID: "+ newjob.getPersistenceId());
//        } else {
//         System.out.println("Check for status and forms error ");
//        }
//
//        //4、--------------Job Submission--------------
//        //Job submission start
//        MSubmission submission = client.startSubmission(newjob.getPersistenceId());
//        System.out.println("Status : " + submission.getStatus());
//        if(submission.getStatus().isRunning() && submission.getProgress() != -1) {
//          System.out.println("Progress : " + String.format("%.2f %%", submission.getProgress() * 100));
//        }
//        System.out.println("Hadoop job id :" + submission.getExternalId());
//        System.out.println("Job link : " + submission.getExternalLink());
//        Counters counters = submission.getCounters();
//        if(counters != null) {
//          System.out.println("Counters:");
//          for(CounterGroup group : counters) {
//            System.out.print("\t");
//            System.out.println(group.getName());
//            for(Counter counter : group) {
//              System.out.print("\t\t");
//              System.out.print(counter.getName());
//              System.out.print(": ");
//              System.out.println(counter.getValue());
//            }
//          }
//        }
//        if(submission.getExceptionInfo() != null) {
//          System.out.println("Exception info : " +submission.getExceptionInfo());
//        }
//
//
//        //Check job status
//        MSubmission submission1 = client.getSubmissionStatus(newjob.getPersistenceId());
//        if(submission1.getStatus().isRunning() && submission1.getProgress() != -1) {
//          System.out.println("Progress : " + String.format("%.2f %%", submission1.getProgress() * 100));
//        }
//
//        //Stop a running job
//        //submission1.stopSubmission(jid);
//
//
//    }
//}